const date = new Date()
const years = []
const months = []
const days = []
const app = getApp()
const util = require('../../utils/util.js')
for (let i = 1949; i <= date.getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    years: years,
    year: date.getFullYear(),
    months: months,
    month: 2,
    days: days,
    day: 2,
    value: [date.getFullYear(), date.getMonth(), date.getDate() - 1],
    currentData: 0,
    hasUserInfo: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // let _this = this
    // wx.getSystemInfo({
    //   success: function(r) {
    //     console.log(r)
    //     _this.setData({
    //       windowHeight: r.windowHeight
    //     })
    //     wx.getSetting({
    //       success(res) {
    //         console.log(wx.getStorageSync('user').user_nickname)
    //         if (!res.authSetting['scope.userInfo'] || wx.getStorageSync('user').user_nickname == undefined) {
    //           _this.setData({
    //             hasUserInfo: false,
    //             loading: true
    //           })
    //         }
    //       }
    //     })
    //     try {
    //       const value = wx.getStorageSync('user')
    //       const state = wx.getStorageSync('state')
    //       if (value) {
    //         // Do something with return value
    //         app.openid = value.openid
    //         app.nickname = value.user_nickname
    //       }
    //       if (state == "1") {
    //         util.issh((res) => {
    //           if (res.data.data.issh == 1) {
    //             wx.redirectTo({
    //               url: '/pages/label/label',
    //             })
    //           } else {
    //             wx.redirectTo({
    //               url: '/pages/qiXi/qiXi',
    //             })
    //           }
    //           console.log(res)
    //         })
    //       } else {
    //         _this.setData({
    //           loading: true
    //         })
    //       }
    //     } catch (e) {
    //       // Do something when catch error
    //       console.log(e)
    //     }
    //   }
    // })
  },
  bindChange: function(e) {
    const val = e.detail.value
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
      day: this.data.days[val[2]]
    })
  },

  //点击切换，滑块index赋值
  checkCurrent: function(e) {
    const that = this;
    if (that.data.currentData === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentData: e.target.dataset.current
      })
    }
  },

  getUserInfo: function(e) {
    const _this = this
    wx.login({
      success: res => {
        console.log(res)
        const preams = {
          code: res.code,
          encrypted_data: e.detail.encryptedData,
          iv: e.detail.iv,
          raw_data: e.detail.rawData,
          signature: e.detail.signature
        }
        util.getUserInfo(preams, function(result) {
          console.log(result)
          app.openid = result.data.data.user.openid
          app.nickname = result.data.data.user.user_nickname
          try {
            wx.setStorageSync('user', result.data.data.user)
          } catch (e) {
            console.log(e)
          }
          if (result.data.code == 1) {
            _this.setData({
              hasUserInfo: true
            })
          }
        })
      }
    })
  },


  gender() {
    const _this = this
    wx.showModal({
      title: '温馨提示',
      content: '请确认信息，确定后不能更改！',
      success: function(res) {
        if (res.confirm) {
          const time = _this.data.value[0] + '-' + (_this.data.value[1] + 1) + '-' + (_this.data.value[2] + 1)
          util.gender(app.openid, _this.data.currentData, time, function(res) {
            console.log(res)
            wx.setStorageSync('state', '1')
            util.issh((res) => {
              if (res.data.data.issh == 0) {
                wx.redirectTo({
                  url: '/pages/label/label',
                })
              } else {
                wx.redirectTo({
                  url: '/pages/qiXi/qiXi',
                })
              }
              console.log(res)
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})